package com.sangeng.ss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sangeng.ss.domain.LoginUser;
import com.sangeng.ss.domain.User;
import com.sangeng.ss.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ：xxx
 * @description：TODO
 * @date ：2024/02/27 17:08
 */

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        User user = userMapper.selectOne(wrapper.eq(User::getUserName, s));
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名不存在");
        }
        // todo 查询权限信息封装List
//        List<String> permissions = Collections.emptyList();
        List<String> permissions = Stream.of("aaa", "bbb", "test", "hello").collect(Collectors.toList());
        return new LoginUser(user, permissions);
    }
}
